package edu.yuhf.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.yuhf.db.utils.DBConnection;
import com.yuhf.db.utils.JdbcTemplate;

import edu.yuhf.dao.iface.UserDao;
import edu.yuhf.domain.User;
import edu.yuhf.domain.UserDetail;

public class UserJdbcDaoImpl implements UserDao {
//SDJADJIASHDIHASIDH
	@Override
	public int checkUser(String name, String password) {
		int rowNum=0;
		String sql="select count(1) from users where name=? and password=?";
		rowNum=JdbcTemplate.queryForCount(sql, name,password);
		return rowNum;
	}

	@Override
	public int insertUser(User user) {
		int userId=-1;
		String sql="insert into users values(users_id.nextval,?,?,?,?)";
		Connection connection=DBConnection.getConnection();
		try {
			PreparedStatement psmt=connection.prepareStatement(sql,new String[] {"id"});
			psmt.setString(1, user.getName());
			psmt.setString(2, user.getPassword());
			psmt.setString(3, user.getSex());
			psmt.setString(4, user.getEmail());
			int rowNum=psmt.executeUpdate();
			ResultSet rs=psmt.getGeneratedKeys();
			if(rowNum==1&&rs.next()) {
				userId=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userId;
	}

	@Override
	public int insertUserDetail(UserDetail ud) {
		int rowNum=0;
		String sql="insert into userDetail values(userdetail_id.nextval,?,?,?)";
		rowNum=JdbcTemplate.update(sql, ud.getNativePlace_code(),ud.getHobby_code(),ud.getUserId());
		return rowNum;
	}

}
